<?php
if($_SESSION['rank'] < 3){
	exit(Lang::getBackGlobal('wrong_permissions'));
}
/**
 * @author Martin Pernica
 * @copyright 2008
 */

if(empty($_GET['name'])){

echo '<table cellpadding="5" style="color: black;" class="clean_table">';
echo '<tr><th>'.Lang::getBackGlobal('modules_name').'</th><th>'.Lang::getBackGlobal('modules_version').'</th><th>'.Lang::getBackGlobal('action').'</th></tr>';
$modules = dirlist('../modules');
for($i = 0; !empty($modules[$i]); $i++){
    if($modules[$i] != "mod_menuTop" && $modules[$i] != "mod_menuLeft" && $modules[$i] != "mod_menuRight" && $modules[$i] != "mod_leftblock" && $modules[$i] != "mod_rightblock" && $modules[$i] != "mod_topblock" && $modules[$i] != "mod_bottomblock"){

    if(is_file('..'.DIR_SEP.'modules'.DIR_SEP.$modules[$i].DIR_SEP.'info.xml')){

    $xml = simplexml_load_file('..'.DIR_SEP.'modules'.DIR_SEP.$modules[$i].DIR_SEP.'info.xml');
    $name = (string)$xml->info[0]->name;
    $version = (string)$xml->info[0]->version;

	}else{
		$name = $modules[$i];
		$version = 'unknown';
	}
	
	if(is_file('..'.DIR_SEP.'modules'.DIR_SEP.$modules[$i].DIR_SEP.'config.xml')){
		$admin = true;
	}else{
		$admin = false;
	}
    $module = explode('_', $modules[$i]);
	$setting = ($admin) ? '<td><a href="'.$_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'].'&name='.$module[1].'">Nastavení</a></td>' : '<td>-</td>';
	echo '<tr onmouseover="this.style.background = \'white\'" onmouseout="this.style.background = \'#f5f5f5\'"><td><strong>'.$name.'</strong></td><td><em>'.$version.'</em></td>'.$setting.'</tr>';
	}
}
echo '</table>';
}
else if(is_file('..'.DIR_SEP.'modules'.DIR_SEP.'mod_'.$_GET['name'].DIR_SEP.'config.xml')){
	
	 echo '<br /><h2><a href="index.php?p=modules">'.Lang::getBackGlobal('back').'</a></h2><br />';
	
	$form = new Form("", "post");
	$xml = new XmlMods('..'.DIR_SEP.'modules'.DIR_SEP.'mod_'.$_GET['name'].DIR_SEP.'config.xml');
	$vars = $xml->getAllVars();
	foreach($vars as $key => $val){
		$form->addElement('input', array('type' => 'text', 'name' => $key, 'value' => $val, 'label' => $key));
	}
	$form->addElement('input', array('type' => 'submit', 'name' => 'xml_save', 'value' => SUBMIT));
	
	if($form->isSubmitted()){
		unset($_POST['xml_save']);
		foreach($_POST as $key => $val){
				$xml->setVar($key, $val);
		}
		$xml->save();
	}
		$form->render();
	
}else{
	echo '<h1>'.Lang::getBackGlobal('modules_nosetting').'</h1>';
}

?>